package com.tech.koufu.ui.view;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.tencent.qq.QQ;

import com.tech.koufu.MainActivity;
import com.tech.koufu.MyApplication;
import com.tech.koufu.R;
import com.tech.koufu.tools.PublicFuc;
import com.tech.koufu.ui.activity.UserDetailActivity;
import com.tech.koufu.ui.activity.login.LoginActivity;
import com.tech.koufu.ui.activity.menu.MyChooseStockActivity;
import com.tech.koufu.ui.activity.menu.MyTrackActivity;
import com.tech.koufu.ui.activity.setting.SettingActivity;

import android.app.Application;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;

/**
 * 
 * @ClassName: MenuLeftFragment
 * @Description: 左边菜单
 * @author 123
 * @date 2015年4月4日 上午10:43:38
 */
public class MenuLeftFragment extends BaseThridLoginFragment {
	private static final String TAG = MenuLeftFragment.class.getName();
	private View rootView;
	private LinearLayout leftmenu_setting;
	private MyApplication myApp;
	private LinearLayout ll_menu_login,ll_menus;
	private RelativeLayout rl_menu_QQ_login;

	@Override
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
		super.onCreateView(inflater, container, savedInstanceState);
		if (rootView == null) {
			rootView = inflater.inflate(R.layout.menu_layout, container, false); 
			init();
			
		}
		ViewGroup parent = (ViewGroup) rootView.getParent();
		if (parent != null) {
			parent.removeView(rootView);
		} 
	
		return rootView; 
	}
	@Override
	public void onActivityCreated(@Nullable Bundle savedInstanceState) { 
		super.onActivityCreated(savedInstanceState);
		Log.d(TAG, "~~~~~onActivityCreated~~~~~~~");
		myApp = (MyApplication) getActivity().getApplication();
		showLeftMenu(myApp.isLogin);
	}

	private void init(){
		leftmenu_setting = (LinearLayout)rootView.findViewById(R.id.ll_leftmenu_setting);
		myOnClickListener myClick = new myOnClickListener() ;
		leftmenu_setting.setOnClickListener(myClick);
		rootView.findViewById(R.id.iv_big_def_head).setOnClickListener(myClick);
		ll_menu_login = (LinearLayout)rootView.findViewById(R.id.ll_menu_left_login);
		ll_menus = (LinearLayout)rootView.findViewById(R.id.ll_item_menus);
		rl_menu_QQ_login =(RelativeLayout) rootView.findViewById(R.id.rl_menu_QQ_login);
		rl_menu_QQ_login.setOnClickListener(myClick);
		
		rootView.findViewById(R.id.rl_menu_myChooseStock).setOnClickListener(myClick);
	}
	public void showLeftMenu(boolean islogin){
		if(islogin){
			ll_menus.setVisibility(View.VISIBLE);
			ll_menu_login.setVisibility(View.GONE);
			leftmenu_setting.setVisibility(View.VISIBLE);
		}else{
			ll_menus.setVisibility(View.GONE);
			ll_menu_login.setVisibility(View.VISIBLE);
			leftmenu_setting.setVisibility(View.GONE);
		}
	}
	private class myOnClickListener implements OnClickListener{

		@Override
		public void onClick(View v) { 
			switch (v.getId()) {
			case R.id.ll_leftmenu_setting:
				Intent intent = new Intent(getActivity(),SettingActivity.class);
				getActivity().startActivityForResult(intent, PublicFuc.CODE_Back);
				break;
			case R.id.iv_big_def_head:
				myApp = (MyApplication) getActivity().getApplication();
				if(myApp.isLogin){
					intent = new Intent(getActivity(),UserDetailActivity.class);
					intent.putExtra("userid", myApp.getDigitalid());
					intent.putExtra("username", myApp.getUserName());
					startActivity(intent);
				}else{
//					intent = new Intent(getActivity(),LoginActivity.class);  
//					startActivity(intent);
					Toast.makeText(getActivity().getApplicationContext(), "请先登录", Toast.LENGTH_SHORT).show();
				}
				break;
			case R.id.rl_menu_QQ_login:
//				intent = new Intent(getActivity(),LoginActivity.class);
//				getActivity().startActivityForResult(intent, PublicFuc.code_login_suss);
				Platform qq = ShareSDK.getPlatform(QQ.NAME); 
				authorize(qq);
				break;
			case R.id.rl_menu_myChooseStock:
				intent = new Intent(getActivity(), MyChooseStockActivity.class); 
				startActivity(intent);
				break;

			default:
				break;
			}
		}
		
	}
}
